//
//  ConfirmOrderViewController.m
//  MeterMart
//
//  Created by ENERGY on 2018/9/21.
//  Copyright © 2018年 ENERGY. All rights reserved.
//

#import "ConfirmComboViewController.h"
#import "PayWayViewController.h"
#import "MallProductDetailViewController.h"
#import "CartViewController.h"
#import "WeChatLoginViewController.h"
@interface ConfirmComboViewController ()
@property (nonatomic, strong)UITableView * tableView;
@property (nonatomic, strong)UIView * headerView;
@property (nonatomic, strong)UIView * topWhiteView;//上部白色背景
@property (nonatomic, strong)UIView * middleWhiteView;//中部白色背景
@property (nonatomic, strong)UIView * bottomWhiteView;//中部白色背景
@property (nonatomic, strong)UILabel * nameLab;//用户名
@property (nonatomic, strong)UILabel * phoneNum;//电话号
@property (nonatomic, strong)UILabel * defaultLab;//默认
@property (nonatomic, strong)UILabel * locationLab;//地址
@property (nonatomic, strong)UIImageView * produceImg;//商品图片
@property (nonatomic, strong)UILabel * produceTitleLab;//商品名
@property (nonatomic, strong)UILabel * priceLab;//金额
@property (nonatomic, strong)UILabel * numLab;//数量
@property (nonatomic, strong)NSString * numString;//数量
@property (nonatomic, strong)UILabel * listPriceLab;//商品订单
@property (nonatomic, strong)NSString * listID;//订单ID
@property (nonatomic, strong)NSDictionary * goodsModel;//商品模型
@property (nonatomic, strong)NSString * totalMoney;//总金额
@property (nonatomic, strong)NSString * addressContentID;//地址ID
@property (nonatomic, strong)UIButton * submitBtn;//提交订单
@property (nonatomic, strong)UIViewController * lastVC;//
@property (nonatomic, strong)UILabel * specificationLab;//规格
@property (nonatomic, strong)NSString * prodeceTitle;//商品名称
@property (nonatomic, strong)NSString * prodeceDescribe;//商品描述
@property (nonatomic, strong)NSDictionary * model;
@property (nonatomic, strong)UILabel * firstListLab;//首单提示
@property (nonatomic, strong)UILabel * inHotelLab;//住店时间
@property (nonatomic, strong)UILabel * outHotelLab;//离店时间
@property (nonatomic, strong)UILabel * totalDayLab;//共住几晚
@property (nonatomic, strong)NSString * phoneString;
@property (nonatomic, strong)NSString * tokenString;
- (void)ifLogin;
@end

@implementation ConfirmComboViewController(networking)
//从套餐详情页进入
- (void)makeSureOrderList{
    NSDictionary * dic = @{
                           @"phone":self.phoneString,
                           @"token":self.tokenString,
                           @"goodsid":self.produceID,
                           @"typeid":self.typeID,
                           @"start":self.startTimeString,
                           @"end":self.endTimeString,
                           @"goodssum":self.dayString
                           };
    [self showprogressHUD];
    [[AFAppDotNetAPIClient sharedClient] POST:@"orderiosApi/createOrder_hotel" parameters:dic progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        [self hiddenProgressHUD];
        if ([responseObject[@"sign"] isEqualToString:@"1"]) {
            self.goodsModel = responseObject[@"list"][@"goodsInfo"];
            self.totalMoney = responseObject[@"list"][@"totalmoney"];
            self.listPriceLab.text = [NSString stringWithFormat:@"需实付款￥%@", responseObject[@"list"][@"totalmoney"]];
            if ([responseObject[@"list"][@"isfirst"] isEqualToString:@"1"]) {
                self.firstListLab.hidden = NO;
            }else{
                self.firstListLab.hidden = YES;
            }
        }else if ([[NSString stringWithFormat:@"%@", responseObject[@"sign"]] isEqualToString:@"400"]) {
            [self ifLogin];
        }else{
            ShowMessage(responseObject[@"msg"]);
        }
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        [self hiddenProgressHUD];
        [self showError:error];
    }];
}
@end
@implementation ConfirmComboViewController
- (void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    [self setStatusBarBackgroundColor:[UIColor clearColor]];
    [self.navigationController setNavigationBarHidden:NO animated:YES];
    if ([[UserModel sharedInstanced].token isEqualToString:@""] || [UserModel sharedInstanced].token == nil || [[UserModel sharedInstanced].token length] == 0 || [[UserModel sharedInstanced].mobile isEqualToString:@""] || [UserModel sharedInstanced].mobile == nil || [[UserModel sharedInstanced].mobile length] == 0) {
        self.tokenString = @"";
        self.phoneString = @"";
    }else{
        self.tokenString = [UserModel sharedInstanced].token;
        self.phoneString = [UserModel sharedInstanced].mobile;
    }
    [self makeSureOrderList];//网络
}
- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = getColor(mainColor);
    self.navigationItem.title = @"确认订单";
    if (@available(iOS 11.0, *)) {
        self.tableView.contentInsetAdjustmentBehavior = UIScrollViewContentInsetAdjustmentNever;
    }else {
        self.automaticallyAdjustsScrollViewInsets = NO;
    }
    [self.view addSubview:self.tableView];
    [self setUpBottomTabBarView];
    
}

#pragma mark - init
- (UITableView *)tableView{
    if (!_tableView) {
        _tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT - NAVI_HEIGHT - BOTTOM_HEIGHT - 49/WIDTH_6_SCALE) style:UITableViewStylePlain];
        _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
        _tableView.backgroundColor = getColor(mainColor);
        _tableView.tableHeaderView = self.headerView;
    }return _tableView;
}
- (UIView *)headerView{
    if (!_headerView) {
        _headerView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT - NAVI_HEIGHT - BOTTOM_HEIGHT - 49/WIDTH_6_SCALE)];
        _headerView.backgroundColor = getColor(mainColor);
        [self setUpMiddleWhiteView];
        if ([self.typeID isEqualToString:@"2"]) {
            [self setUpDataModule];
        }
    }return _headerView;
}
- (void)setUpMiddleWhiteView{
    //中部白色背景
    self.middleWhiteView = [[UIView alloc] init];
    self.middleWhiteView.backgroundColor = [UIColor whiteColor];
    [_headerView addSubview:self.middleWhiteView];
    //商品图片
    self.produceImg = [[UIImageView alloc] init];
    self.produceImg.image = [UIImage imageNamed:@"YimiPlaceHolder"];
    [self.middleWhiteView addSubview:self.produceImg];
    //商品名
    self.produceTitleLab = [[UILabel alloc] init];
    [self label:self.produceTitleLab withFont:DEF_FontSize_14 withText:@"商品名称" withTextColor:[UIColor blackColor] withTextAlignment:NSTextAlignmentLeft withView:self.middleWhiteView];
    self.produceTitleLab.numberOfLines = 0;
    self.produceTitleLab.lineBreakMode = 0;
    //商品价格
    self.priceLab = [[UILabel alloc] init];
    [self label:self.priceLab withFont:DEF_FontSize_15 withText:[NSString stringWithFormat:@"￥%@", @"---"] withTextColor:getColor(mainRedColor) withTextAlignment:NSTextAlignmentLeft withView:self.middleWhiteView];
    //数量
    self.numLab = [[UILabel alloc] init];
    [self label:self.numLab withFont:DEF_FontSize_15 withText:[NSString stringWithFormat:@"X  %@", @"--"] withTextColor:getColor(textGrayColor) withTextAlignment:NSTextAlignmentRight withView:self.middleWhiteView];
    //规格
    self.specificationLab = [[UILabel alloc] init];
    [self label:self.specificationLab withFont:DEF_FontSize_15 withText:@"" withTextColor:getColor(textGrayColor) withTextAlignment:NSTextAlignmentCenter withView:self.middleWhiteView];
    //首单提示
    self.firstListLab = [[UILabel alloc] init];
    [self label:self.firstListLab withFont:DEF_FontSize_10 withText:@"*首单立减50元" withTextColor:getColor(mainRedColor) withTextAlignment:NSTextAlignmentLeft withView:_headerView];
    //masonry
    __weak typeof (self)weakSelf = self;
    [self.middleWhiteView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.top.right.equalTo(weakSelf.headerView);
        make.height.mas_offset(150/WIDTH_6_SCALE);
    }];
    [self.produceImg mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(weakSelf.middleWhiteView.mas_left).mas_offset(25/WIDTH_6_SCALE);
        make.top.equalTo(weakSelf.middleWhiteView.mas_top).mas_offset(23/WIDTH_6_SCALE);
        make.width.height.mas_offset(111/WIDTH_6_SCALE);
    }];
    [self.firstListLab mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(weakSelf.produceImg.mas_left);
        make.top.equalTo(weakSelf.middleWhiteView.mas_bottom).mas_offset(5/WIDTH_6_SCALE);
    }];
    [self.produceTitleLab mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(weakSelf.produceImg.mas_right).mas_offset(30/WIDTH_6_SCALE);
        make.top.equalTo(weakSelf.middleWhiteView.mas_top).mas_offset(26/WIDTH_6_SCALE);
        make.right.equalTo(weakSelf.middleWhiteView.mas_right).mas_offset(-16/WIDTH_6_SCALE);
        make.bottom.lessThanOrEqualTo(weakSelf.priceLab.mas_top).mas_offset(-10/WIDTH_6_SCALE);
    }];
    [self.specificationLab mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.bottom.equalTo(weakSelf.priceLab);
        make.left.equalTo(weakSelf.priceLab.mas_right).priorityHigh();
        make.right.equalTo(self.numLab.mas_left);//must be low priority
    }];
    [self.priceLab mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(weakSelf.produceTitleLab.mas_left);
        make.bottom.equalTo(weakSelf.middleWhiteView.mas_bottom).mas_offset(-32/WIDTH_6_SCALE);
    }];
    [self.priceLab setContentCompressionResistancePriority:UILayoutPriorityFittingSizeLevel forAxis:UILayoutConstraintAxisHorizontal];//宽度不够时，可以被压缩
    [self.priceLab setContentHuggingPriority:UILayoutPriorityRequired/*抱紧*/
                                     forAxis:UILayoutConstraintAxisHorizontal];
    [self.numLab mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.equalTo(weakSelf.middleWhiteView.mas_right).mas_offset(-18/WIDTH_6_SCALE);
        make.top.bottom.equalTo(weakSelf.priceLab);
    }];
    [self.numLab setContentCompressionResistancePriority:UILayoutPriorityFittingSizeLevel forAxis:UILayoutConstraintAxisHorizontal];//宽度不够时，可以被压缩
    [self.numLab setContentHuggingPriority:UILayoutPriorityRequired/*抱紧*/
                                   forAxis:UILayoutConstraintAxisHorizontal];
}
- (void)setUpDataModule{
    UIButton * dataBGView = [[UIButton alloc] initWithFrame:CGRectMake(0, 150/WIDTH_6_SCALE, SCREEN_WIDTH, 50/WIDTH_6_SCALE)];
    [dataBGView setBackgroundColor:[UIColor whiteColor]];
    [_headerView addSubview:dataBGView];
    [self setBorderWithView:dataBGView top:NO left:NO bottom:YES right:NO borderColor:getColor(@"f7f7f7") borderWidth:1];
    //中间横线长度
    int middleLineW = 10/WIDTH_6_SCALE;
    //住店日期
    self.inHotelLab = [[UILabel alloc] initWithFrame:CGRectMake(18/WIDTH_6_SCALE, 0, SCREEN_WIDTH/4, dataBGView.height)];
    self.inHotelLab.text = self.startTimeString;
    self.inHotelLab.font = DEF_FontSize_14;
    self.inHotelLab.textAlignment = NSTextAlignmentRight;
    [dataBGView addSubview:self.inHotelLab];
    //中间横线
    UIView * middleLineView = [[UIView alloc] initWithFrame:CGRectMake(self.inHotelLab.right + 10/WIDTH_6_SCALE, dataBGView.height/2 - 1/WIDTH_6_SCALE, middleLineW, 2/WIDTH_6_SCALE)];
    middleLineView.backgroundColor = [UIColor grayColor];
    [dataBGView addSubview:middleLineView];
    //离店日期
    self.outHotelLab = [[UILabel alloc] initWithFrame:CGRectMake(middleLineView.right + 10/WIDTH_6_SCALE, 0, self.inHotelLab.width, self.inHotelLab.height)];
    self.outHotelLab.text = self.endTimeString;
    self.outHotelLab.font = DEF_FontSize_14;
    self.outHotelLab.textAlignment = NSTextAlignmentLeft;
    [dataBGView addSubview:self.outHotelLab];
    //共住几天
    self.totalDayLab = [[UILabel alloc] initWithFrame:CGRectMake(self.outHotelLab.right + 20/WIDTH_6_SCALE, 0, 80/WIDTH_6_SCALE, dataBGView.height)];
    self.totalDayLab.textAlignment = NSTextAlignmentLeft;
    self.totalDayLab.text = @"共1晚";
    self.totalDayLab.font = DEF_FontSize_12;
    [dataBGView addSubview:self.totalDayLab];
}
- (void)setUpBottomTabBarView{
    UIView * bottomBar = [[UIView alloc] init];
    bottomBar.backgroundColor = [UIColor whiteColor];
    [self.view addSubview:bottomBar];
    //总价
    self.listPriceLab = [[UILabel alloc] init];
    [self label:self.listPriceLab withFont:DEF_FontSize_14 withText:[NSString stringWithFormat:@"需实付款￥---"] withTextColor:[UIColor blackColor] withTextAlignment:NSTextAlignmentRight withView:bottomBar];
    //提交订单
    UIButton * submitBtn = [[UIButton alloc] init];
    submitBtn.titleLabel.font = DEF_FontSize_15;
    [submitBtn setTitle:@"提交订单" forState:UIControlStateNormal];
    [submitBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    [submitBtn setBackgroundColor:getColor(@"e92600")];
    [submitBtn addTarget:self action:@selector(submitBtnClick:) forControlEvents:UIControlEventTouchUpInside];
    self.submitBtn = submitBtn;
    [bottomBar addSubview:submitBtn];
    //masonry
    __weak typeof (self)weakSelf = self;
    [bottomBar mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.equalTo(weakSelf.view);
        make.bottom.equalTo(weakSelf.view.mas_bottom).mas_offset(-BOTTOM_HEIGHT);
        make.height.mas_offset(49/WIDTH_6_SCALE);
    }];
    [self.listPriceLab mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.equalTo(submitBtn.mas_left).mas_offset(-3/WIDTH_6_SCALE);
        make.top.bottom.equalTo(bottomBar);
    }];
    [submitBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.bottom.right.equalTo(bottomBar);
        make.width.mas_offset(107/WIDTH_6_SCALE);
    }];
}
- (void)setGoodsModel:(NSDictionary *)goodsModel{
    _goodsModel = goodsModel;
    self.produceTitleLab.text = [NSString stringWithFormat:@"%@  %@", goodsModel[@"goodsname"], goodsModel[@"goodsdescribe"]];
    self.prodeceDescribe = goodsModel[@"goodsdescribe"];
    self.prodeceTitle = goodsModel[@"goodsname"];
    [self.produceImg sd_setImageWithURL:[NSURL URLWithString:goodsModel[@"goodsimg"]] placeholderImage:[UIImage imageNamed:@"YimiPlaceHolder"]];
    self.numString = goodsModel[@"goodssum"];
    self.priceLab.text = [NSString stringWithFormat:@"￥%@", goodsModel[@"groupprice"]];
    self.listID = goodsModel[@"ordernum"];
    if ([goodsModel[@"handlength"] isEqualToString:@"0"] || [goodsModel[@"handlength"] length] == 0 || [goodsModel[@"handlength"] isEqualToString:@""] || goodsModel[@"handlength"] == nil) {
        self.specificationLab.text = @"";
    }else{
        self.specificationLab.text = goodsModel[@"handlength"];
    }
    self.numLab.text = [NSString stringWithFormat:@"X  %@", goodsModel[@"goodssum"]];
}
#pragma mark - action
- (void)arrowCoverBtnClick:(UIButton *)sender{
    NSLog(@"箭头");
}
- (void)submitBtnClick:(UIButton *)sender{
    PayWayViewController * vc = [[PayWayViewController alloc] init];
    vc.listID = self.goodsModel[@"ordernum"];
    vc.produceTitle = self.prodeceTitle;
    vc.produceDescribe = self.prodeceDescribe;
    [self.navigationController pushViewController:vc animated:YES];
}
- (void)ifLogin{
    WeChatLoginViewController * vc = [[WeChatLoginViewController alloc] init];
    UINavigationController* navi = [[UINavigationController alloc] initWithRootViewController:vc];
    vc.backHomePageBlock = ^{
        [vc dismissViewControllerAnimated:YES completion:nil];
        [self.tabBarController setSelectedIndex:0];
        [self.navigationController popToRootViewControllerAnimated:YES];
    };
    [self presentViewController:navi animated:YES completion:nil];
}
#pragma mark - 单边边框
- (void)setBorderWithView:(UIView *)view top:(BOOL)top left:(BOOL)left bottom:(BOOL)bottom right:(BOOL)right borderColor:(UIColor *)color borderWidth:(CGFloat)width{
    if (top) {
        CALayer *layer = [CALayer layer];
        layer.frame = CGRectMake(0, 0, view.frame.size.width, width);
        layer.backgroundColor = color.CGColor;
        [view.layer addSublayer:layer];
    }
    if (left) {
        CALayer *layer = [CALayer layer];
        layer.frame = CGRectMake(0, 0, width, view.frame.size.height);
        layer.backgroundColor = color.CGColor;
        [view.layer addSublayer:layer];
    }
    if (bottom) {
        CALayer *layer = [CALayer layer];
        layer.frame = CGRectMake(0, view.frame.size.height - width, view.frame.size.width, width);
        layer.backgroundColor = color.CGColor;
        [view.layer addSublayer:layer];
    }
    if (right) {
        CALayer *layer = [CALayer layer];
        layer.frame = CGRectMake(view.frame.size.width - width, 0, width, view.frame.size.height);
        layer.backgroundColor = color.CGColor;
        [view.layer addSublayer:layer];
    }
}
@end
